<?php

namespace Admin\Controller;

use Think\Controller;

header('Content-type:text/html;charset=utf-8');

class LinkController extends BaseController {

	private $create_fields = array('name', 'url','create_time','update_time','state','orderby');
	private $edit_fields   = array('name', 'url','create_time','update_time','state','orderby');

	public function index() {
		
		$Link = D('Link');		
		$count = $Link -> count();
		$p = getpage($count, C('PAGE_SIZE'));
		$show = $p -> show();
		$list = $Link -> page(I('get.p')) -> limit(C('PAGE_SIZE')) ->order('id desc')-> select();
		$this -> assign('list', $list);
		$this -> assign('page', $show);
		$this -> display();
	}

	public function add() {
		$obj = D('Link');
		
		if (IS_POST) {
			$data = $this -> createCheck();

			if ($obj -> add($data)) {
				$this -> success("保存成功", U('Link/index'));
				return;
			} else {
				$this -> error('操作失败！');
				return;
			}
		}
		$this -> display();
	}

	private function createCheck() {

		$param = I('post.');
		$data = $this -> checkFields($param["data"], $this -> create_fields);
		$create_time = strtotime(date("Y-m-d H:i:s", time()));
		$data['name'] = trim(I('post.name', '', 'htmlspecialchars'));					
		$data['url'] = trim(I('post.url', '', 'htmlspecialchars'));
		$data['state'] = trim(I('post.state', '', 'htmlspecialchars'));
		$data['orderby'] = trim(I('post.orderby', '', 'htmlspecialchars'));
		$data['create_time'] = $create_time;
		if ($data['name'] == null) {
			$this -> error('名称不能为空');
			return;
		}
		if ($data['url'] == null) {
			$this -> error('域名不能为空');
			return;
		}
		if ($data['orderby'] == null) {
			$this -> error('排序不能为空');
			return;
		}
		return $data;
	}

	public function edit($id = 0) {
		
		$obj = D('Link');		
		if ($id = (int)$id) {
			if (!$detail = $obj -> find($id)) {
				$this -> error('请选择要编辑的说说');
			}
			if (IS_POST) {
				$data = $this -> editCheck();
				$data['id'] = $id;
				if ($obj -> save($data)) {
					$this -> success('操作成功', U('Link/index'));
					return;
				} else {
					$this -> error('操作失败');
					return;
				}
			} else {
				$this -> assign('detail', $detail);
				$this -> display();
			}
		} else {
			$this -> error('请选择要编辑的说说');
		}
	}

	private function editCheck() {

		$param = I('post.');
		$data = $this -> checkFields($param['data'], $this -> edit_fields);
		$update_time = strtotime(date("Y-m-d H:i:s", time()));
		$data['name'] = trim(I('post.name', '', 'htmlspecialchars'));					
		$data['url'] = trim(I('post.url', '', 'htmlspecialchars'));
		$data['state'] = trim(I('post.state', '', 'htmlspecialchars'));
		$data['orderby'] = trim(I('post.orderby', '', 'htmlspecialchars'));
		$data['update_time'] = $update_time;
		if ($data['name'] == null) {
			$this -> error('名称不能为空');
			return;
		}
		if ($data['url'] == null) {
			$this -> error('域名不能为空');
			return;
		}
		if ($data['orderby'] == null) {
			$this -> error('排序不能为空');
			return;
		}
		return $data;
	}


	public function del() {
		$Link = D('Link');
		$id = I('get.id');
		$result = $Link -> where('id =' . $id . '') -> delete();
		if ($result) {
			$this -> success('删除成功', U('Link/index'));
		} else {
			$this -> error('删除失败');
		}
	}

}
